package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Category;
import org.vectrics.common.html.Combo;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.user.Person;

abstract public class PeopleCombo extends Combo {

	private static final long serialVersionUID = -3014317151523043692L;
	
	static private Category log = Category.getInstance(PeopleCombo.class);
    private String styleClass = null;
    private String property = null;
    private String defaultToCurrentUser = "true";
    

    public PeopleCombo() {
//    	addPeople();
    }

    abstract protected boolean getAddNoPersonOption();
	abstract protected Collection findPersons();
	
	public void addPeople() {
		log.debug("doStartTag() called");
		Collection persons = findPersons();
		
		Person authUser = ThreadProperties.getPerson();
		Long authUserId = null;
		if (authUser != null) {
			authUserId = authUser.getId(); 
		}

		Iterator iter = persons.iterator();
		
		if (this.showBlank()) {
			this.addOption("", "");			
		}
		
		while (iter.hasNext()) {
		    Person person = (Person)iter.next();
		    this.addOption(person.getName(), person.getId().toString());
		}

	}

	

	protected boolean showBlank() {
		return false;
	}

	private boolean isSelectedInPersonList(Long selectedUserId, Collection persons) {
	    boolean result = false;
	    Iterator iter = persons.iterator();
	    while (iter.hasNext()) {
	        Person person = (Person)iter.next();
	        if (person.getId().equals(selectedUserId)) {
	            result = true;
	            break;
	        }
	    }
	    
	    return(result);
	}
	
	
	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}


	/**
	 * @return Returns the defaultToCurrentUser.
	 */
	public String getDefaultToCurrentUser() {
		return defaultToCurrentUser;
	}
	/**
	 * @param defaultToCurrentUser The defaultToCurrentUser to set.
	 */
	public void setDefaultToCurrentUser(String defaultToCurrentUser) {
		this.defaultToCurrentUser = defaultToCurrentUser;
	}
	
}
